<?php
namespace App\Models;

use Illuminate\Http\Request;

/**
 * 商品表
 */
class MProduct extends BaseModel
{
    public $table = "products";

    public static $STATUS_SALE = 0; //商品在售中
    public static $STATUS_DOWN = 1; //商品已下架


    /**
     * 添加一件商品
     * $img = 商品封面图片相对路径
     */
    public function addOne(Request $request, $img)
    {
        if (empty($this->no)) {
            $this->no = onlyNo();
        }
        $this->title = $request->title;
        if (!empty($img)) {
            $this->img = $img;
        }
        $this->p1 = $request->price1;
        $this->p2 = $request->price2;
        $this->p3 = $request->price3;
        $this->p4 = $request->price4;
        $this->p5 = $request->price5;
        $this->detail = $request->detail;
        $this->unit = $request->unit;
        $this->status = $request->status;
        $this->num = $request->num;
        $this->time = time();
        return $this->save();
    }


    /**
     * 商品编辑数据合法性验证
     * $flag 0 = rules  1 = msg
     */
    public static function editRulesWithMsg($flag)
    {
        $rules = array(
            "title"  => "required|between:2,100",
            'status' => "required|numeric",
            'img'    => 'image',
            'p1'     => 'numeric',
            'p2'     => 'numeric',
            'p3'     => 'numeric',
            'p4'     => 'numeric',
            'p5'     => 'numeric',
            'unit'   => 'required|numeric',
            'num'    => 'required|numeric',
            'detail' => 'required'
        );

        $msg = array(
            "unit.required" => "请必须填写商品的最少购买次数！",
            "num.required"  => "请必须填写商品的库存！",
        );

        if ($flag == 0) {
            return $rules;
        } else {
            return $msg;
        }
    }
}